<script setup lang="ts">
import { get, loadFile, message, post } from '@/utils'
import useModal from '@/hooks/useModal'
import useTable from '@/hooks/useTable'
import MenuModal from './components/MenuModal.vue'
import { BtnPermission } from '@/enum/permission'
import RoleModal from './components/RoleModal.vue'
import useRole from '@/hooks/useRole'
const { list, getList, remove } = useRole()
const { store: menuStore } = useModal()
const { store } = useModal()
const { formatColumns } = useTable()
const searchQuery = reactive({})
const formRef = ref()
const actionRef = ref()
const request = (params: any, sort: any, filters: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    const query = { ...params, size: params.pageSize }
    Reflect.deleteProperty(query, 'pageSize')
    get({ url: '/server-system/admin/role/list' }, ({ data }) => {
      resolve({
        data: data,
        total: data.length,
        success: true,
      })
    })
  })
}

const columns = formatColumns([
  {
    title: '角色名称',
    dataIndex: 'roleName',
  },
  {
    title: '角色别名',
    dataIndex: 'roleAlias',
  },
  {
    title: '角色排序',
    dataIndex: 'sort',
  },
  {
    title: '操作',
    dataIndex: 'operations',
    btnList: [
      {
        name: '编辑',
        permission: BtnPermission.编辑,
        handle: ({ record }) => store.handleShow(record),
      },
      {
        name: '权限设置',
        permission: BtnPermission.编辑,
        handle: ({ record }) => menuStore.handleShow(record),
      },
      {
        name: '删除',
        type: 'popconfirm',
        tip: '确认删除该条数据吗?',
        permission: BtnPermission.删除,
        handle: ({ record }) => {
          post({ url: '/server-system/admin/role/remove?ids=' + record.id }, ({ code, msg }) => {
            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
    ],
  },
])
</script>

<template>
  <div class="page">
    <ProTable :columns="columns" :request="request" row-key="id" :form-ref="ref => (formRef = ref)" :pagination="false" header-title="" :scroll="{ x: 1000 }" column-resizable :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <a-button @click="store.handleShow({})" type="primary" v-permission="[BtnPermission.新建]">
          <template #icon>
            <icon-plus />
          </template>
          新建</a-button
        >
      </template>
    </ProTable>
    <MenuModal v-if="menuStore.show" v-model:show="menuStore.show" :id="menuStore.id" @confirm="actionRef.reload" />
    <RoleModal v-if="store.show" v-model:show="store.show" :id="store.id" @confirm="actionRef.reload" />
  </div>
</template>
